/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.transform;

import java.util.Iterator;

import alloy.ast.ASTDepthFirstReplacer;
import alloy.ast.BinaryExpr;
import alloy.ast.BinaryExprOp;
import alloy.ast.Expr;
import alloy.type.RelationType;
import alloy.util.Dbg;

/**
 * <p>A visitor which desugars the restriction operators :> and <:.  The
 * domain restriction operator is desugared as described below
 * <p>Suppose r has the type (T11 -> T12 -> ... -> T1n) + ... + (Tk1 -> Tk2 -> ... -> Tkn)
 * and s has the type (T1 + T2 + ... + Tp).  We desugar s<:r as 
 * (((s&Ti1) -> Ti2 -> ... -> Tin) +...+ ((s&Tj1) -> Tj2 -> ... -> Tjn)) & r
 * <p>Range restriction operator is desugared analogously:
 * <p>Suppose r has the type (T11 -> T12 -> ... -> T1n) + ... + (Tk1 -> Tk2 -> ... -> Tkn)
 * and s has the type (T1 + T2 + ... + Tp).  We desugar r:>s as 
 * ((Ti1 -> Ti2 -> ... -> (s&Tin)) +...+ (Tj1 -> Tj2 -> ... -> (s&Tjn))) & r
 */
public class DesugarRestrictionVisitor extends ASTDepthFirstReplacer {


    public DesugarRestrictionVisitor() {}

    /**
     * Suppose r has the type (T11 -> T12 -> ... -> T1n) + ... + (Tk1 -> Tk2 -> ... -> Tkn)
     * and s has the type (T1 + T2 + ... + Tp).  Suppose that m is some integer such that
     * 1<=m<=n.  Then, the method returns the expression
     * ((Ti1 -> ... -> (s&Tim)  -> ... -> Tin) +...+ (Tj1 -> ... -> (s&Tjm) -> ... -> Tjn)) & r
     * Note that the returned expression contains only the constraints of the form (Tj1 -> ... -> (s&Tjm) -> ... -> Tjn)
     * for RelationTypes whose mth BasicType intersects with s.
     **/
    private static Expr getRestrictedExpr(Expr r, Expr s, int m) {
	Expr retExpr = null; 

	Iterator relTypeIter = r.getType().getTypeIter();
	
	while (relTypeIter.hasNext()) {
	    RelationType relType = (RelationType) relTypeIter.next();
	    // if s and Tim don't intersect, omit the constraint (Tj1 -> ... -> (s&Tjm) -> ... -> Tjn)
	    if (s.getType().intersect(relType.getBasicTypeAt(m).toExpr().getType()).isEmpty())
		continue;

	    Expr constraint = null;
	    for (int i = 0; i < relType.numBasicTypes(); i++) {
		Expr curExpr = relType.getBasicTypeAt(i).toExpr(); // Tji
		if (i == m) { // if i == m, we need to form the intersection s&Tjm
		    curExpr = ((Expr) s.copy()).intersect(curExpr);
		}
		// (Tj1 -> ... -> Tj(i-1) -> Tji
		constraint = (constraint == null ? curExpr : constraint.product(curExpr));
	    }
	    
	    // now, we add the constraint for the jth RelationType to retExpr
	    retExpr = (retExpr == null ? constraint : retExpr.union(constraint));
	}
	// finally, we intersect retExpr with r:  
	// ((Ti1 -> ... -> (s&Tim)  -> ... -> Tin) +...+ (Tj1 -> ... -> (s&Tjm) -> ... -> Tjn)) & r
	return retExpr.intersect(r);
    }

    /** Desugares a restriction expression.  The code assumes that the
     * expression has already been typechecked. **/
    public Object visit(BinaryExpr binExpr_) {
		Expr left = (Expr) binExpr_.getLeft().applyReturnVisitor(this);
		binExpr_.setLeft(left);
		    
		Expr right = (Expr) binExpr_.getRight().applyReturnVisitor(this);
		binExpr_.setRight(right);
		
		Dbg.check(left.getType() != null && right.getType() != null);
		BinaryExprOp op = binExpr_.getOp();
		
		if (op.equals(BinaryExprOp.DOMAINRESTRICT)) {
		    return getRestrictedExpr(right, left, 0);
		} else if (op.equals(BinaryExprOp.RANGERESTRICT)) {
		    return getRestrictedExpr(left, right, left.getType().arity()-1);
		} else {
		    return binExpr_; // not a restriction operator, nothing to do . . .
		}
	
    }
}
